/*Copyright ©2015 TommyLemon(https://github.com/TommyLemon)

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.*/

package soft.hb.activity;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;

import com.alibaba.fastjson.JSONObject;

import java.util.ArrayList;
import java.util.List;

import soft.hb.R;
import soft.hb.model.Project;
import soft.hb.model.Vehicle;
import soft.hb.util.HttpRequest;
import zuo.biao.library.base.BaseActivity;
import zuo.biao.library.interfaces.OnHttpResponseListener;
import zuo.biao.library.ui.DatePickerWindow;
import zuo.biao.library.util.JSON;
import zuo.biao.library.util.TimeUtil;

/**
 * 编辑设备信息
 *
 * @author yuanweiwei
 */
public class ModifyActivity extends BaseActivity implements OnClickListener, OnHttpResponseListener {
    public static final String TAG = "ModifyActivity";

    /**
     * 获取启动IndexActivity的intent
     *
     * @param context
     * @return
     */
    public static Intent createIntent(Context context) {
        return new Intent(context, ModifyActivity.class);
    }


    @Override
    public Activity getActivity() {
        return this; //必须return this;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.modify_activity);
        intent = getIntent();
        //功能归类分区方法，必须调用<<<<<<<<<<
        initView();
        initData();
        initEvent();
        //功能归类分区方法，必须调用>>>>>>>>>>

    }


    //UI显示区(操作UI，但不存在数据获取或处理代码，也不存在事件监听代码)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

    private EditText editVoucherNo;		// 凭证号码
    private EditText editDetectionTime;		// 检测时间
    private EditText editKAvg;		//  K(均值)



    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    @Override
    public void initView() {//必须调用
        editVoucherNo = findView(R.id.edit_voucherNo);
        editKAvg = findView(R.id.edit_KAvg);
        editDetectionTime = findView(R.id.edit_detectionTime);
        editDetectionTime.setOnClickListener(this);

        findView(R.id.btn_submit).setOnClickListener(this);

    }


    //UI显示区(操作UI，但不存在数据获取或处理代码，也不存在事件监听代码)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


    //Data数据区(存在数据获取或处理代码，但不存在事件监听代码)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<



    @Override
    public void initData() {//必须调用

    }

    //Data数据区(存在数据获取或处理代码，但不存在事件监听代码)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


    //Event事件区(只要存在事件监听代码就是)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

    @Override
    public void initEvent() {//必须调用

    }


    //系统自带监听方法<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    private int[] selectedDate = new int[]{1971, 0, 1};
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_submit:
                Vehicle vehicle = new Vehicle();
                vehicle.setId(getIntent().getStringExtra("id"));

                if(editVoucherNo.getText() == null || editVoucherNo.getText().toString().equals("")){
                    showShortToast("凭证号不能为空！");
                    return;
                }
                    if(editKAvg.getText() == null || editKAvg.getText().toString().equals("")){
                    showShortToast("Kavg不能为空！");
                    return;
                }
                    if(editDetectionTime.getText() == null || editDetectionTime.getText().toString().equals("")){
                    showShortToast("检测时间不能为空！");
                    return;
                }

                vehicle.setDetectionTime(editDetectionTime.getText().toString());
                vehicle.setKAvg(editKAvg.getText().toString());
                vehicle.setVoucherNo(editVoucherNo.getText().toString());
                showProgressDialog(R.string.loading);
                HttpRequest.modifyVehicle(vehicle,0, new OnHttpResponseListener() {
                    @Override
                    public void onHttpResponse(int requestCode, String resultJson, Exception e) {
                        JSONObject jsonObject = JSON.parseObject(resultJson);
                        if(jsonObject.getBoolean("success")){
                            showShortToast("保存成功！");
                            setResult(RESULT_OK, intent);
                            finish();
                        }else{
                            showShortToast(jsonObject.getString("msg"));
                        }
                        dismissProgressDialog();
                    }
                });


                break;

            case R.id.edit_detectionTime:
                toActivity(DatePickerWindow.createIntent(context, new int[]{1971, 0, 1}
                        , TimeUtil.getDateDetail(System.currentTimeMillis())), REQUEST_TO_DATE_PICKER, false);
                break;
        }
    }



    //类相关监听<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

    private static final int REQUEST_TO_DATE_PICKER = 33;

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != RESULT_OK) {
            return;
        }
        switch (requestCode) {

            case REQUEST_TO_DATE_PICKER:
                if (data != null) {
                    ArrayList<Integer> list = data.getIntegerArrayListExtra(DatePickerWindow.RESULT_DATE_DETAIL_LIST);
                    if (list != null && list.size() >= 3) {

                        selectedDate = new int[list.size()];
                        for (int i = 0; i < list.size(); i++) {
                            selectedDate[i] = list.get(i);
                        }
                        editDetectionTime.setText(selectedDate[0] + "-" + (selectedDate[1] + 1) + "-" + selectedDate[2]);
                    }
                }
                break;


        }
    }


    @Override
    public void finish() {
        super.finish();

    }

    @Override
    public void onHttpResponse(int requestCode, String resultJson, Exception e) {

    }


    //类相关监听>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    //系统自带监听方法>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


    //Event事件区(只要存在事件监听代码就是)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

}